package com.xiaoyu.client.adapter.mall;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.goods.MallHomeBean.DataBean.*;
import com.xiaoyu.commonlib.ui.OnItemRecyClickListener;
import com.xiaoyu.commonlib.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

public class MallRecomAdapter extends RecyclerView.Adapter<MallRecomAdapter.ViewHolder> {
    private Context mContext;
    private List<RelistBean> mList = new ArrayList<>();
    private OnItemRecyClickListener mListener;

    public MallRecomAdapter(Context mContext, List<RelistBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_imgs,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        if (position == 0){
            Glide.with(mContext).load(mList.get(position).getGoodslbimg()).into(holder.newImg);
            holder.newImg.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(175)));
        }else {
            Glide.with(mContext).load(mList.get(position).getGoodsimg()).into(holder.newImg);
            holder.newImg.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,UIUtils.dip2px(105)));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onItemRecyClick(holder.itemView,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView newImg;
        public ViewHolder(View itemView) {
            super(itemView);
            newImg = itemView.findViewById(R.id.item_img);
        }
    }

    public void updateList(List<RelistBean> mList){
        this.mList = mList;
        this.notifyDataSetChanged();
    }

    public void setOnItemRecyClickListener(OnItemRecyClickListener mListener) {
        this.mListener = mListener;
    }
}
